package shopping;

import java.util.ArrayList;
import connection.GoodsDAO;

public class Action {
	public static ArrayList<Goods> arr = new ArrayList<Goods>();
	private double totle;
	GoodsDAO goodsdao = new GoodsDAO();

	public void setTotle(double totle) {
		this.totle = totle;
	}

	public double getTotle() {
		return totle;
	}

	public void addGoods(int num) {
		Goods it = goodsdao.getItByNum(num);
		if (arr.size() == 0)
			arr.add(it);
		else if (arr.size() > 0) {
			int i;
			for (i = 0; i < arr.size(); i++) {
				Goods good = arr.get(i);
				if (good.getNum() != it.getNum())
					continue;
				else
					good.addAmount();
				break;
			}
			if (i == arr.size())
				arr.add(it);
		}
		totalPrice();
	}

	public void removeGoods(int num) {
		Goods it = goodsdao.getItByNum(num);
		if (arr.size() > 0) {
			for (int i = 0; i < arr.size(); i++) {
				Goods good = arr.get(i);
				if (good.getNum() == it.getNum()) {
					if (good.getAmount() == 1)
						arr.remove(i);
					else
						good.reduceAmount();
				}
			}
		}
		totalPrice();
	}

	public double totalPrice() {
		double sum = 0.0;
		for (int i = 0; i < arr.size(); i++) {
			Goods good = arr.get(i);
			sum += good.getPrice() * good.getAmount();
		}
		this.setTotle(sum);
		return this.getTotle();
	}

}
